package com.cn.lyl.lawyer.service.impl;

import com.cn.lyl.lawyer.dao.UserMapper;
import com.cn.lyl.lawyer.model.entity.UserModel;
import com.cn.lyl.lawyer.service.UserService;
import com.cn.lyl.lawyer.util.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public String checkLogin(UserModel userModel) {
        if(userModel.getCode().equals(userModel.getVerityCode())){
            UserModel model = userMapper.findByUsernameAndPassword(userModel);
            if(model == null)
            {
                return "error";
            }
            else{
                if (model.getEnable()==0){
                    return "lock";
                }
                return "success";
            }
        }
        return "error-code";
    }

    @Override
    public List<UserModel> findAll(UserModel userModel) {
        return userMapper.findAll(userModel);
    }

    @Override
    public void add(UserModel userModel) {

        userModel.setWeight(Tool.floatIfNotNull(userModel.getWeight()));
        userModel.setAddress(Tool.stringIsNotNull(userModel.getAddress()));
        userModel.setEmail(Tool.stringIsNotNull(userModel.getEmail()));
        userMapper.add(userModel);
    }

    @Override
    public void delete(Integer id) {
        userMapper.delete(id);
    }

    @Override
    public String changeInfo(UserModel userModel) {
        if(findByUserName(userModel)!=null){
            return "repeat";
        }
        userMapper.updateInfo(userModel);
        return ("success");
    }

    @Override
    public String changePass(UserModel userModel) {
        return null;
    }

    @Override
    public UserModel findById(Integer id) {
        return userMapper.findById(id);
    }

    @Override
    public void changeEnable(Integer id) {
        userMapper.updateEnable(id);
    }

    @Override
    public int getCount(UserModel usermodel) {
        return userMapper.countFindAll(usermodel);
    }

    private UserModel findByUserName(UserModel userModel){
        return userMapper.findByUserName(userModel);
    }
}
